package org.springframework.security.core.session;

import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.util.Assert;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;

/**
 * @author Dillon
 * @date 2024/7/14
 * @slogan 致敬大师 致敬未来的你
 * @desc session控制 实体类
 */
public class SessionInformation implements Serializable {

	@Serial
	private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

	/**
	 * 最后请求日期
	 */
	private Date lastRequest;

	/**
	 * session主体 session必要的认证对象
	 */
	private final Object principal;

	/**
	 * sessionID
	 */
	private final String sessionId;

	/**
	 * session是否过期标志位
	 */
	private boolean expired = false;

	/**
	 * 构造函数
	 *
	 * @param principal   session主体
	 * @param sessionId   sessionID
	 * @param lastRequest 最后请求时间
	 */
	public SessionInformation(Object principal, String sessionId, Date lastRequest) {
		Assert.notNull(principal, "Principal required");
		Assert.hasText(sessionId, "SessionId required");
		Assert.notNull(lastRequest, "LastRequest required");
		this.principal = principal;
		this.sessionId = sessionId;
		this.lastRequest = lastRequest;
	}

	/**
	 * 标记当前session过期
	 */
	public void expireNow() {
		this.expired = true;
	}

	/**
	 * 获取session最后请求执行时间
	 *
	 * @return session请求时间
	 */
	public Date getLastRequest() {
		return this.lastRequest;
	}

	public Object getPrincipal() {
		return this.principal;
	}

	public String getSessionId() {
		return this.sessionId;
	}

	public boolean isExpired() {
		return this.expired;
	}

	/**
	 * 刷新session最后请求时间
	 */
	public void refreshLastRequest() {
		this.lastRequest = new Date();
	}

}
